#include "StdMystring.h"
#include <cstring>
StdMyString::StdMyString()
{
    capacity = 15;
    size = 0;
    string = new char[capacity];
}

StdMyString::StdMyString(const char *str)
{

    size = strlen(str);
    capacity = size + 1;
    string = new char[capacity];
    strcpy(string, str);
    std::cout << "带参构造\n";
}

StdMyString::StdMyString(const StdMyString &str)
{
    size = str.size;
    capacity = str.capacity;
    string = new char[capacity];
    strcpy(string, str.string);
}

StdMyString::~StdMyString()
{
    delete[] string;
}

StdMyString& StdMyString::operator=(const StdMyString &str)
{
    delete[] string;
    size = str.size;
    capacity = str.capacity;
    string = new char[capacity];
    strcpy(string, str.string);
    return *this;
}

StdMyString StdMyString::operator+(const StdMyString &str)
{   
    StdMyString s=*this;
    s+=str;
    return  s ;
}

StdMyString &StdMyString::operator+=(const StdMyString &str)
{
    size=size+str.size;
    if (capacity<=size)
    {    capacity=size+1;
         char *newp=new char[capacity];
        strcpy(newp,string);
        delete[]string;
        string=newp;
    }
    strcat(string,str.string);
    return *this;
}

StdMyString &StdMyString::operator-=(const StdMyString &str)
{   
    char*dest=strstr(string,str.string);
    while (dest!=nullptr)
    {
        char*back=dest+str.size;
        while (*back!='\0')
        {
            *dest++=*back++;
        }
        *dest='\0';
        dest=strstr(string,str.string);
    }
    return *this;
}

StdMyString StdMyString::operator-(const StdMyString &str)
{
    StdMyString s=*this;
    s-=str;
    return  s ; 
}

char &StdMyString::operator[](int index)
{   

    return string[index];
}

StdMyString &StdMyString::operator+=(const char &str)
{   
 if (capacity<=size)
    {   capacity*=2;
        char *newp=new char[capacity];
        strcpy(newp,string);
        delete[]string;
        string=newp;
    }
    
    string[size++]=str;
    string[size]='\0';

    return *this;
}

bool StdMyString::operator==(const StdMyString &str)
{   
    return strcmp(string,str.string)==0;
}


bool StdMyString::operator>(const StdMyString &str)
{   
    return strcmp(string,str.string)>0;
}

bool StdMyString::operator<(const StdMyString &str)
{   
    return strcmp(string,str.string)<0;
}

StdMyString::operator int()
{
    return atoi(string);

}

StdMyString::operator double()
{
    return atof(string);
}

MyStringList StdMyString::split(const StdMyString &str)
{   StdMyString Cpstr=*this;
    MyStringList list;
    char* dest=strtok(Cpstr.string,str.string);
    while (dest!=nullptr)
    {
        list+=dest;
        dest=strtok(nullptr,str.string);
    }
    return list;
   
}

// bool StdMyString::operator&&(const StdMyString &str)
// {
//     return size!=0&&str.size!=0;
// }

std::ostream &operator<<(std::ostream &os, const StdMyString &str)
{
    os << str.string;
    return os;
}

std::istream &operator>>(std::istream &is, StdMyString &str)
{   char temp=0;
    str.size=0;
    //从终端获取一个字符
  while ((temp=getchar())!='\n')
  {
    str+=temp;
  }
  return is;
}

std::ostream &operator<<(std::ostream &os, const MyStringList &list)
{
    for (int i = 0; i < list.size; i++)
    {
        os<<list.array[i]<<std::endl;
    }
    return os;
}

MyStringList::MyStringList(int capacity):size(0),capacity(capacity)
{
    array=new StdMyString[capacity];

}

MyStringList &MyStringList::operator+=(const StdMyString &str)
{
    if(size>=capacity)
    {
        capacity*=2;
        StdMyString*newp=new StdMyString[capacity];
        for (int i = 0; i < size; i++)
        {
            newp[i]=array[i];

        }
        delete[]array;
        array=newp;
    }

    array[size++]=str;

return *this;

}

MyStringList &MyStringList::operator<<(const StdMyString &str)
{
    *this+=str;
    return *this;
}

MyStringList MyStringList::operator+(const MyStringList &list)
{   
    MyStringList s;
    for (int i=0; i < size; i++)
    {
        s+=array[i];
    }
        for (int i=0; i < size; i++)
    {
        s+=list.array[i];
    }
return s;

}

MyStringList::~MyStringList()
{
    delete[]array;
}

MyStringList MyStringList::operator-=(const MyStringList &list)
{   



    return ;
}
